package dom;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.QName;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author yuqi<307530884@qq.com> <2017/4/3>
 * @version 1.0
 * @since 1.0
 */
public class MavenPomReader {
    private File file;

    private Map<String, Element> map = new HashMap<String, Element>();

    public MavenPomReader(File file) {
        this.file = file;
    }

    private MavenPomReader writeVersion2Properties(Filter filter, Parse parse){
        try (InputStream inputStream = new FileInputStream(file)) {
            // 读取输入流
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            // 得到XML根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();

            // 遍历所有子节点
            List<Element> proList = new ArrayList<>();
            for (Element e : elementList) {
                map.put(e.getName(), e);
                if(filter.filte(e)){
                    proList = parse.parse(e);
                }
            }

            System.out.println(proList.size());

            if(proList != null && proList.size() > 0){
                for(Element pro : proList ){
                    root.element("properties").add(pro);
                }
            }

            if(proList.size() > 0){
                // 输出全部原始数据，并用它生成新的我们需要的XML文件
                XMLWriter writer2 = new XMLWriter(new FileWriter(file));
                writer2.write(document); //输出到文件
                writer2.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return this;
    }

    public Map<String, Element> getMap() {
        return map;
    }

    public static void main(String[] args) {
        final File file = new File("H:\\workspaces\\workspace_mars2_master\\jk\\jk-parent\\pom.xml");
        MavenPomReader mavenPomReader = new MavenPomReader(file);
        Map<String, Element> map = mavenPomReader.writeVersion2Properties(e -> {
            if ("dependencyManagement".equals(e.getName())) {
                return true;
            }
            return false;
        }, dependencyManagement -> {
            List<Element> proList = new ArrayList<>();

            // 得到根元素的所有子节点
            List<Element> dependenciesList = dependencyManagement.elements();
            // 遍历所有子节点
            for (Element dependencies : dependenciesList) {
                List<Element> dependencyList = dependencies.elements();
                // 遍历所有子节点

                for (Element e : dependencyList) {
                    String artifactId = e.elementTextTrim("artifactId");
                    String version = e.elementTextTrim("version");

                    if(!version.startsWith("${")){
                        System.out.println(artifactId + ":" + version);
                        String proName = artifactId + ".version";

                        String xmlTagStart = "<"+proName+">";
                        String xmlTagEnd = "</"+proName+">";
                        String proAll = xmlTagStart+version+xmlTagEnd;

                        String depVersion = "${"+proName+"}";

                        e.element("version").setText(depVersion);

                        Element element = new DOMElement(proName);
                        element.setText(version);
                        element.setQName(new QName(proName, dependencyManagement.getNamespace()));

                        proList.add(element);
                    }
                }
            }

            return proList;
        }).getMap();
        System.out.println(map.keySet());
    }
}

interface Filter {
    boolean filte(Element e);
}

interface Parse {
    List<Element> parse(Element e);
}
